<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Files extends Controller_Autorization {

	public function before() {
		parent::before();
		$this->_json = FALSE;
		$this->_short = FALSE;
	}

	public function after() {
		if ($this->_json === TRUE) {
			$this->response->headers('Content-Type', 'text/json charset=utf-8');
			$this->_body = json_encode($this->_body);
		}
		elseif ($this->_short === FALSE) {
			$this->_body = View::factory('blocks/header_bs', $this->_data) .
					View::factory('blocks/header_bs_menu', $this->_data) .
					$this->_body .
					View::factory('blocks/footer_bs');
		}
		$this->response->body($this->_body);
		parent::after();
	}

	/**/

	public function action_upload() {
		$this->_json = TRUE;
		//print_r($_POST);
		if ($this->request->method() == Request::POST) {
			if (!empty($_POST['files_path'])) {
				switch ($_POST['files_content_type']) {
					case 'image/jpeg':
						$ext = '.jpg';
						break;
					case 'image/png':
						$ext = '.png';
						break;
					case 'image/gif':
						$ext = '.gif';
						break;
					default:
						$ext = '.jpg';
				}
				//echo DOCROOT.'---'.$_POST['files_path'];
				$filename = str_replace(array(DOCROOT . 'tmp/', '/'), array('', '_'), $_POST['files_path']) . $ext;
				Model::factory('Book')->into_files(array('itemid' => $this->_data['orderid'], 'path' => $_POST['files_path'] . $ext));
				$this->_body = array('data' => array('uploadname' => $filename, 'itemid' => $this->_data['orderid']));
			}
		}


		//$view->error_message = $error_message;
		//$this->response->body($view);
	}

	public function action_photoupload() {
		$this->_json = TRUE;

		if ($this->request->method() == Request::POST) {
			$item = Model::factory('Book')->get_data('items', NULL, $this->_data['itemid'], TRUE);
			if (!empty($item['report'])) {
				$report = explode(',', trim($item['report'], ','));
				$report[] = $_POST['name'];
				$report = array_unique($report);
				foreach ($report as $k => &$v) {
					$v = trim($v);
					if (empty($v)) {
						unset($report[$k]);
					}
				}
			}
			else {
				$report[] = $_POST['name'];
			}
			Model::factory('Book')->update('items', array('id' => $this->_data['itemid']), array('report' => implode(',', $report)));

			$this->_body = array('data' => array('count' => count($report), 'report' => implode(',', $report), 'itemid' => $this->_data['itemid']));
		}
	}

	protected function _save_image($image, $w, $h) {
		if (
				!Upload::valid($image) OR
				!Upload::not_empty($image) OR
				!Upload::type($image, array('jpg', 'jpeg', 'png', 'gif'))) {
			return FALSE;
		}

		$directory = DOCROOT . 'uploads/' . $this->_data['orderid'];
		if (!is_dir($directory)) {
			mkdir($directory, 0755, TRUE);
		}
		if ($file = Upload::save($image, NULL, $directory)) {
			$filename = substr(md5(microtime(TRUE)), 0, 16) . '.jpg';
			/*
			  Image::factory($file)
			  ->resize(1600, 1200,Image::AUTO)
			  ->save($directory.'/'.$filename);

			  // Delete the temporary file
			  unlink($file);
			 */
			return $filename;
		}

		return FALSE;
	}

}

// End Images
